package com.jason.platform.supervision.activity;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.Toast;
import com.hzlj.supervision.changsha.R;
import com.jason.platform.supervision.adapter.HomeModelAdapter;
import com.jason.platform.supervision.beans.ModelBean;
import com.jason.platform.supervision.beans.UserInfoBean;
import com.jason.platform.supervision.utils.SharedPreferencesUtils;
import com.jason.platform.supervision.widght.TitleBar;
import org.kymjs.aframe.ui.KJActivityManager;

/**
 * @ClassName: HomeActivity
 * @Description: HomeActivity 主页面Activity
 * @Author: Jason.Zhang zhangyujn1989ok@gmail.com
 * @Date: 2015-7-25 06:56:09
 */
public class HomeActivity extends BaseActivity {

    private static final String TAG = HomeActivity.class.getSimpleName();

    public static final String KEY_MODEL_TITLE = "key_model_title";

    private GridView mHomeGridView;
    private HomeModelAdapter mHomeModelAdapter;
    private TitleBar mTitleBar;
    private long mExitTime;

    @Override
    void handleMessage(BaseActivity activity, Message msg) {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_home);
        super.onCreate(savedInstanceState);
        mTitleBar = (TitleBar) findViewById(R.id.title_bar);
        mTitleBar.bindActivity(this);
        UserInfoBean userInfoBean = SharedPreferencesUtils.getUserInfoBean(this);
        if (userInfoBean != null && !TextUtils.isEmpty(userInfoBean.getUserName())){
            mTitleBar.setUserName(userInfoBean.getUserName());
        }

        mHomeGridView = (GridView) findViewById(R.id.home_gridview);
        mHomeModelAdapter= new HomeModelAdapter(this);
        mHomeGridView.setAdapter(mHomeModelAdapter);

        mHomeGridView.setOnItemClickListener(new HomeModelItemClickListener());
        mHomeGridView.setSelector(new ColorDrawable(Color.TRANSPARENT));
    }

    private class HomeModelItemClickListener implements AdapterView.OnItemClickListener {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                                long id) {
            ModelBean bean = (ModelBean) parent.getItemAtPosition(position);
            Log.i(TAG,"HomeModelItemClickListener title name :" + bean.getTitle());
            Class modelClass = null;
            switch (bean.getIconId()) {
                case R.drawable.ic_home_module_notify:
                    //  1.通知
                    modelClass = NotifyActivity.class;
                    break;
                case R.drawable.ic_home_module_record:
                    // 2.电子档案
                    modelClass = RecordActivity.class;
                    break;
                case R.drawable.ic_home_module_monitor:
                    //  3.在线监控
                    modelClass = MonitorActivity.class;
                    break;
                case R.drawable.ic_home_module_locate:
                    //  4.人员定位
                    modelClass = LocateActivity.class;
                    break;
                case R.drawable.ic_home_module_communication:
                    //  5.实时通讯
                    modelClass = CommunicationActivity.class;
                    break;
                case R.drawable.ic_home_module_interview:
                    //  6.工作走访
                    modelClass = InterviewActivity.class;
                    break;
                case R.drawable.ic_home_module_returning:
                    //  7.电话回访
                    modelClass = ReturningActivity.class;
                    break;
                case R.drawable.ic_home_module_service:
                    //  8.社区服务
                    modelClass = ServiceActivity.class;
                    break;
                case R.drawable.ic_home_module_education:
                    //  9.集中教育
                    modelClass = EducationActivity.class;
                    break;
                case R.drawable.ic_home_module_statistics:
                    //  10.统计分析
                    modelClass = StatisticsActivity.class;
                    break;
                case R.drawable.ic_home_module_config:
                    //  11.系统配置
                    modelClass = ConfigActivity.class;
//                    modelClass = LeaveApprovalActivity.class;
                    break;
                case R.drawable.ic_home_module_about:
                    //  12.关于我们
                    modelClass = AboutActivity.class;
                    break;

                case R.drawable.ic_home_module_playback:
                    //  7.轨迹回放
                    modelClass = PlayBackActivity.class;
                    break;
                case R.drawable.ic_home_module_leavesign:
                    //  8.请假签到
//                    modelClass = LeaveSignActivity.class;
                    modelClass = LeaveActivity.class;
                    break;
                case R.drawable.ic_home_module_dailysign:
                    //  10.日常签到
                    modelClass = DailySignActivity.class;
                    break;
                default:
                    break;
            }
            Intent intent = new Intent(HomeActivity.this,modelClass);
            intent.putExtra(KEY_MODEL_TITLE,bean.getTitle());
            startActivity(intent);
        }

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK
                && event.getAction() == KeyEvent.ACTION_DOWN) {
            if ((System.currentTimeMillis() - mExitTime) > 2000) {
                Toast.makeText(this, "再按一次退出应用",
                        Toast.LENGTH_SHORT).show();
                mExitTime = System.currentTimeMillis();
            } else {
                KJActivityManager.create().AppExit(HomeActivity.this);
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
